import React, { PureComponent } from 'react';
import { Modal, Form, Input } from 'antd';

const FormItem = Form.Item;

@Form.create()
class AddGHModal extends PureComponent {
  state = {};

  handleOk = e => {
    const { form, onOk, data } = this.props;
    e.preventDefault();
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (data) {
          onOk(values, data.id);
        } else {
          onOk(values);
        }
      }
    });
  };

  handleCancel = () => {
    this.props.onCancel();
  };

  render() {
    const { visible, form, data } = this.props;
    const { getFieldDecorator } = form;

    return (
      <Modal
        title="添加公众号"
        visible={visible}
        style={{ top: 200 }}
        closable={false}
        cancelText="取消"
        okText="确定"
        onOk={this.handleOk}
        onCancel={this.handleCancel}
      >
        <Form layout="inline">
          <FormItem label="公众号名称">
            {getFieldDecorator('ghname', { initialValue: data ? data.name : '' })(
              <Input placeholder="请输入" style={{ width: 300 }} />
            )}
          </FormItem>
          <FormItem label="原始ID">
            {getFieldDecorator('ghid', {
              rules: [{ required: true, message: '请填写原始ID' }],
              initialValue: data ? data.ghid : '',
            })(<Input placeholder="请输入" style={{ width: 300 }} />)}
          </FormItem>
          <FormItem label="appid">
            {getFieldDecorator('appid', {
              rules: [{ required: true, message: '请填写appid' }],
              initialValue: data ? data.appid : '',
            })(<Input placeholder="请输入" style={{ width: 300 }} />)}
          </FormItem>
          <FormItem label="appsecret">
            {getFieldDecorator('secret', {
              rules: [{ required: true, message: '请填写appsecret' }],
              initialValue: data ? data.secret : '',
            })(<Input placeholder="请输入" style={{ width: 300 }} />)}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default AddGHModal;
